package cn.ac.iscas.dao.impl.dameng;

import cn.ac.iscas.dao.IJobMetadataDao;
import cn.ac.iscas.dao.condition.DamengDataSourceCondition;
import cn.ac.iscas.dao.mapper.dameng.IDamengJobMetadataMapper;
import cn.ac.iscas.domain.job.Job;
import cn.ac.iscas.domain.job.JobSearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
@Conditional(DamengDataSourceCondition.class)
public class DamengJobMetadataDao implements IJobMetadataDao {
    @Autowired
    private IDamengJobMetadataMapper jobMetadataMapper;

    public List<Job> select(JobSearch jobSearch) {
        return jobMetadataMapper.select(jobSearch);
    }

    public List<Job> selectByGroup(JobSearch jobSearch) {
        return jobMetadataMapper.selectJobByGroup(jobSearch);
    }

    public List<Job> selectAll() {
        return jobMetadataMapper.selectAll();
    }


    public int save(Job job) {
        return jobMetadataMapper.insert(job);
    }

    @Override
    public int upsert(Job job) {
        return jobMetadataMapper.insert(job);
    }

    public int save(List<Job> jobs) {
        return jobMetadataMapper.insertBatch(jobs);
    }

    public int update(Job job) {
        return jobMetadataMapper.insert(job);
    }

    public boolean delete(String id) {
        int result = jobMetadataMapper.deleteByPrimaryKey(id);
        if (result == 1 || result == 0) {
            return true;
        }
        return false;
    }

    @Override
    public int selectCount(String column) {
        return jobMetadataMapper.selectCount(column);
    }


    public boolean deleteBatch(List<String> ids) {
        int result = jobMetadataMapper.deleteBatch(ids);
        if (result <= ids.size()) {
            return true;
        }
        return false;
    }


    public Job getJob(String id) {
        return jobMetadataMapper.selectById(id);
    }

    public List<Job> selectByIds(List<String> ids) {
        return jobMetadataMapper.selectByIds(ids);
    }

    public List<Job> getPreJobs(String id) {
        return jobMetadataMapper.selectPreJob(id);
    }

    public int updateJobStatus(String id, String status) {
        return jobMetadataMapper.updateStatus(id, status);
    }

    public int updateJobStatusDuration(String id, String status, Long duration) {
        return jobMetadataMapper.updateStatusDuration(id, status, duration);
    }

    public int updateJobExecuteTime(String id, Date executeTime) {
        return jobMetadataMapper.updateExecuteTime(id, executeTime);
    }
}